﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AutoShutdownDotNet.Models;

namespace AutoShutdownDotNet
{
    public partial class TaskForm : Form
    {

        #region Properties

        
        public bool InsertMode
        {
            get { return (TaskId == 0); }
        }
        
        public int TaskId { get; set; }

        public string Title
        {
            get { return txtTitle.Text.Trim(); }
            set { txtTitle.Text = value; }
        }

        public DateTime? When
        {
            get
            {
                if (dpWhen.IsNull) return null;
                return dpWhen.SelectedDateTime;
            }
            set { if (value != null) dpWhen.SelectedDateTime = value.Value; }
        }

        public int Hour
        {
            get { return (int) numHour.Value; }
            set { numHour.Value = value; }
        }

        public int Minute
        {
            get { return (int) numMinute.Value; }
            set { numMinute.Value = value; }
        }

        public AutoShutdownAction Action 
        { 
            get
            {
                if (btnPowerOff.Checked)
                    return AutoShutdownAction.PowerOff;
                else if (btnReboot.Checked)
                    return AutoShutdownAction.Reboot;
                else if (btnLogoff.Checked)
                    return AutoShutdownAction.Logoff;
                else if (btnStandby.Checked)
                    return AutoShutdownAction.Standby;
                else if (btnHibernate.Checked)
                    return AutoShutdownAction.Hibernate;
                else if (btnRun.Checked)
                    return AutoShutdownAction.RunApplication;
                else if (btnShowMessage.Checked)
                    return AutoShutdownAction.ShowMessage;
                else if (btnLock.Checked)
                    return AutoShutdownAction.Lock;
                else
                    return AutoShutdownAction.PowerOff;
            }
            set
            {
                switch (value)
                {
                    case AutoShutdownAction.Hibernate:
                        btnHibernate.Checked = true;
                        break;
                    case AutoShutdownAction.Logoff:
                        btnLogoff.Checked = true;
                        break;
                    case AutoShutdownAction.PowerOff:
                        btnPowerOff.Checked = true;
                        break;
                    case AutoShutdownAction.Reboot:
                        btnReboot.Checked = true;
                        break;
                    case AutoShutdownAction.RunApplication:
                        btnRun.Checked = true;
                        break;
                    case AutoShutdownAction.ShowMessage:
                        btnShowMessage.Checked = true;
                        break;
                    case AutoShutdownAction.Standby:
                        btnStandby.Checked = true;
                        break;
                    case AutoShutdownAction.Lock:
                        btnLock.Checked = true;
                        break;
                    default:
                        btnPowerOff.Checked = true;
                        break;
                }
            } 
        }

        public string FilePath
        {
            get { return txtFilePath.Text; }
            set { txtFilePath.Text = value; }
        }

        public string Message
        {
            get { return txtMessage.Text; }
            set { txtMessage.Text = value; }
        }

        public string Comment
        {
            get { return txtComment.Text; }
            set { txtComment.Text = value; }
        }

        public bool AskBeforeAnyAction
        {
            get { return chkAsk.Checked; }
            set { chkAsk.Checked = value; }
        }

        public int Timer
        {
            get { return (int) numTimer.Value; }
            set { numTimer.Value = value; }
        }

        public bool Active
        {
            get { return chkActive.Checked; }
            set { chkActive.Checked = value; }
        }

        public Task ResultTask { get; set; }

        #endregion

        public TaskForm()
        {
            InitializeComponent();
            TaskId = 0;
        }

        public TaskForm(int taskId)
        {
            InitializeComponent();
            TaskId = taskId;
            Task task = Task.Load(taskId);
            if(task != null)
            {
                Title = task.Title;
                if (task.When.HasValue)
                    When = task.When.Value;
                Hour = task.Hour;
                Minute = task.Minute;
                Action = task.Action;
                AskBeforeAnyAction = task.AskBefore;
                Active = task.Active;
                Timer = task.Timer;
                FilePath = task.FilePath;
                Message = task.Message;
                Comment = task.Comment;
            }
        }

        private void btnRun_CheckedChanged(object sender, EventArgs e)
        {
            txtFilePath.Enabled = btnOpenFile.Enabled = btnRun.Checked;
        }

        private void btnShowMessage_CheckedChanged(object sender, EventArgs e)
        {
            lblMsg.Visible = btnShowMessage.Checked;
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Applications (*.exe)|*.exe|All files(*.*)|*.*";
            openDialog.CheckFileExists = true;
            if(openDialog.ShowDialog() == DialogResult.OK)
            {
                FilePath = openDialog.FileName;
            }
        }

        private bool ValidateInputs()
        {
            errorProvider.Clear();

            if (Title == string.Empty)
                Title = "Untitled Task";
           
            if(btnRun.Checked)
                if(FilePath == string.Empty)
                {
                    errorProvider.SetError(txtFilePath,"Enter or select file path to run");
                    txtFilePath.Focus();
                    return false;
                }
                else
                {
                    if(!File.Exists(FilePath))
                    {
                        MessageBox.Show("The file you have selected to run actually not exists.", "Run Application",
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        txtFilePath.Focus();
                        return false;
                    }
                }

            if(btnShowMessage.Checked)
                if(Message == string.Empty)
                {
                    MessageBox.Show("Please enter something in 'Mesage' tab.", "Show Message", MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
                    Tabs.SelectedTab = tabMessage;
                    return false;
                }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(ValidateInputs())
            {
                Task task = new Task();
                task.Action = this.Action;
                task.Active = this.Active;
                task.AskBefore = this.AskBeforeAnyAction;
                task.Comment = this.Comment;
                if(InsertMode)
                    task.CreatedDate = DateTime.Now;
                task.FilePath = FilePath;
                task.Hour = Hour;
                task.LastModified = DateTime.Now;
                task.Message = Message;
                task.Minute = Minute;
                task.Timer = Timer;
                task.Title = Title;
                if (When != null) task.When = When.Value;

                if (task.Exist())
                {
                    MessageBox.Show("Another task with the same date and time already exists." + Environment.NewLine +
                                    "You can't do multiple taks in a same time, sorry!", "Task Exists",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                    numHour.Focus();
                    return;
                }
                if (InsertMode)
                    task.Insert();
                else
                {
                    task.Id = TaskId;
                    task.Update();
                }

                ResultTask = task;

                DialogResult = DialogResult.OK;
                Close();
            }
        }
    }
}
